package com.tian.util;

import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GeodeticCalculator;
import org.gavaghan.geodesy.GeodeticCurve;
import org.gavaghan.geodesy.GlobalCoordinates;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * {@code @description:} 通过经纬度计算距离工具类
 *
 * @author tianwc 公众号：Java后端技术全栈
 * 在线刷题 1200+java面试题和1000+篇技术文章：<a href="https://woaijava.cc/">博客地址</a>
 * {@code @date:} 2025-01-20 16:10
 * {@code @version:} 1.0
 */
public class LatitudeAndLongitudeDistanceUtil {

    public static BigDecimal getDistance(double lat1, double lng1, double lat2, double lng2) {
        GeodeticCurve geoCurve = new GeodeticCalculator().calculateGeodeticCurve(
                Ellipsoid.Sphere,
                new GlobalCoordinates(lat1, lng1),
                new GlobalCoordinates(lat2, lng2)
        );
        geoCurve.getEllipsoidalDistance();
        BigDecimal s = BigDecimal.valueOf(geoCurve.getEllipsoidalDistance());
        s = s.divide(new BigDecimal(1000), 2, RoundingMode.HALF_UP);
        return s;
    }

    public static void main(String[] args) {
        BigDecimal distance = getDistance(22.678611, 113.805695, 22.716473, 113.826391);
        System.out.println(distance);
    }
}
